package com.store.app.imlife.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hyphenate.easeui.widget.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.store.app.R;
import com.store.app.imlife.bean.UserUrlBean;

import java.util.List;

public class UserAdapter extends BaseAdapter {
    private List<UserUrlBean> List;
    private Context context;
    public UserAdapter(List<UserUrlBean> list,Context context) {
        this.context = context;
        this.List = list;
    }
    public void setList(List<UserUrlBean> List){
        this.List=List;
    }
    @Override
    public int getCount() {
        return List.size()+2;
    }

    @Override
    public Object getItem(int position) {
        return List.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_user, null);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.item_tv);
            holder.circleImageView = (CircleImageView) convertView.findViewById(R.id.iv_head);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if(position<List.size()) {
            UserUrlBean userUrlBean = List.get(position);
            holder.tvTitle.setText(List.get(position).getNickname());
             String head=userUrlBean.getHead_pic_path();
            if (TextUtils.isEmpty(head)) {
                head="null";
            }
            ImageLoader.getInstance().displayImage(head, holder.circleImageView,options);
        }else{
           if(position==List.size()){
               holder.circleImageView.setBackgroundResource(R.drawable.chat_group_add);
           }else if(position==List.size()+1){
               holder.circleImageView.setBackgroundResource(R.drawable.chat_group_min);
           }
        }
        return convertView;
    }
    class ViewHolder {
        TextView tvTitle;
        CircleImageView circleImageView;
    }
    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .showImageOnLoading(R.drawable.ease_default_avatar)
            .showImageOnFail(R.drawable.ease_default_avatar)
            .showStubImage(R.drawable.ease_default_avatar)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
}
